﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HyEduOnline.Entities;

using HyEduOnline.Models;

using HyEduOnline.Server.Common;

using HyEduOnline.Server.Admin;
using HyEduOnline.Server.Student;
using System.Web.Http;
using System.Net;
using System.Net.Http;


namespace WebSite.Areas.WebApi.Controllers
{
    public class NoticesExtController : NoticesController
    {


        private NoticesDAO AtDao = new NoticesDAO();
        private NoticesContentDAO MaDao = new NoticesContentDAO();
        private NoticesReaderDAO ReDao = new NoticesReaderDAO();
        //
        // GET: /Api/NoticesExt/

        public ActionResult Index()
        {
            return View();
        }

        #region 创建新公告+CreatNotice
        /// <summary>
        /// 新建公告
        /// </summary>
        //[ValidateInput(false)]
        public void CreatNotice(NewNotices notice)
        {


            //向公告表插入数据
            TbNotices tbNotice = new TbNotices();
            //test11.13
            //notice.SSenderNo = FrontUserInfo.GetUserNo();
            //notice.SSenderName = FrontUserInfo.GetUserName();
            notice.SSenderNo = FrontUserInfo.GetSysUserName();
            notice.SSenderName = FrontUserInfo.GetSysUserTureName();
            tbNotice.SSenderNo = notice.SSenderNo;
            tbNotice.SSenderName = notice.SSenderName;
            tbNotice.STitle = notice.STitle;
            tbNotice.DSendtime = notice.DSendtime;
            tbNotice.INoticesType = notice.INoticesType;
            tbNotice.SSendIp = notice.SSendIp;
            AtDao.Insert(tbNotice);

            //获取朱表的id
            var id = tbNotice.Id;

            //向公告内容表插入数据
            if (notice.SContent != null)
            {

                TbNoticesContent tbContenet = new TbNoticesContent();

                tbContenet.INoticesId = id;
                tbContenet.SContent = notice.SContent;
                MaDao.Insert(tbContenet);
            }

            //向NoticesReader表插入数据

            TbNoticesReader tbReader = new TbNoticesReader();

            tbReader.INoticesId = id;
            tbReader.IIsRead = 0;//默认公告未读
            ReDao.Insert(tbReader);
        } 
        #endregion




        #region 返回指定Id的公告+GetNotices
        /// <summary>
        ///返回指定id的数据
        /// </summary>
        /// <param name="id">id,主键</param>
        public JsonResult GetNotices(int id)
        {
            NewNotices item = new NewNotices();
            TbNotices notices = new NoticesDAO().GetByID(id);

            if (notices != null)
            {
                item.Id = notices.Id;

                item.SSenderNo = notices.SSenderNo;
                item.SSenderName = notices.SSenderName;
                item.STitle = notices.STitle;
                item.DSendtime = notices.DSendtime;
                item.INoticesType = notices.INoticesType;
                item.SSendIp = notices.SSendIp;

                var NoticesId = notices.Id;
                string WhereClause = "I_NoticesID='" + NoticesId + "'";
                TList<TbNoticesContent> NoticesContent = new NoticesContentDAO().GetByPage(WhereClause, 0, int.MaxValue);
                if (NoticesContent.Count > 0)
                {
                    item.SContent = NoticesContent[0].SContent;
                }
            }
            return Json(item, JsonRequestBehavior.AllowGet);

        } 
        #endregion



        #region 批量删除公告+DeleteNotices
        /// <summary>
        /// DELETE api/Notice/Delete/?ids="1,2,3,4"
        /// </summary>
        public void DeleteNotices(string ids)
        {
            try
            {
                string[] SIds = ids.Split(new char[] { ',' });

                foreach (var SId in SIds)
                {
                    int IId = int.Parse(SId);//对应表的Id

                    TbNotices NoticesList = new NoticesDAO().GetByID(IId);//获取到Notices表Id对应的那条数据

                    if (NoticesList != null)
                    {

                        //构建查询条件
                        string whereClause = "";
                        whereClause = "I_NoticesID=" + IId;

                        //从NoticesContent人表中删除该公告的内容记录
                        TList<TbNoticesContent> NoticesContent = new NoticesContentDAO().GetByPage(whereClause, 0, int.MaxValue);
                        if (NoticesContent.Count > 0)
                        {

                            MaDao.Del(NoticesContent[0]);


                        }
                        //从NoticesReader表中删除记录
                        TList<TbNoticesReader> NoticesReaderList = new NoticesReaderDAO().GetByPage(whereClause, 0, int.MaxValue);
                        if (NoticesReaderList.Count > 0)
                        {

                            ReDao.Del(NoticesReaderList[0]);

                        }
                        AtDao.Del(NoticesList);
                    }




                }

            }
            catch (Exception ex)
            {


            }

        }
        #endregion




        #region 更新公告+Update
        /// <summary>
        /// Update api/Notice/Update/5,更新数据
        /// </summary>
        public int Update(int id, [FromBody]NewNotices item)
        {

            if (id != item.Id)
            {
                return 0;
            }

            try
            {
                //TbNotices Notices = new TbNotices();
                TbNotices Notices = new NoticesDAO().GetByID(id);
               
                //test11.14
                //Notices.SSenderNo = FrontUserInfo.GetUserNo();
                //Notices.SSenderName = FrontUserInfo.GetUserName();
                Notices.SSenderNo = FrontUserInfo.GetSysUserName();
                Notices.SSenderName = FrontUserInfo.GetSysUserTureName();
                Notices.STitle = item.STitle;
                Notices.DSendtime = DateTime.Now;
                Notices.INoticesType = item.INoticesType;
                Notices.SSendIp = item.SSendIp;

                AtDao.Update(Notices);

                //构建查询条件
                string WhereClause = "I_NoticesID='" + Notices.Id + "'";

                TList<TbNoticesContent> NoticesContentList = new NoticesContentDAO().GetByPage(WhereClause, 0, int.MaxValue);
                //如果找到，则更新
                if (NoticesContentList.Count > 0)
                {
                    NoticesContentList[0].SContent = item.SContent;
                    MaDao.Update(NoticesContentList[0]);
                }
                //否则，添加
                else
                {
                    TbNoticesContent NoticesContent = new TbNoticesContent();
                    NoticesContent.INoticesId = Notices.Id;
                    NoticesContent.SContent = item.SContent;
                    MaDao.Insert(NoticesContent);
                }


                TList<TbNoticesReader> NoticesReaderList = new NoticesReaderDAO().GetByPage(WhereClause, 0, int.MaxValue);
                //如果找到，则更新
                if (NoticesReaderList.Count > 0)
                {
                    NoticesReaderList[0].IIsRead = 0;
                    NoticesReaderList[0].INoticesId = Notices.Id;
                    ReDao.Update(NoticesReaderList[0]);
                }
                //否则，添加
                else
                {
                    TbNoticesReader NoticesReader = new TbNoticesReader();
                    NoticesReader.INoticesId = Notices.Id;
                    NoticesReader.IIsRead = 0;
                    ReDao.Insert(NoticesReader);
                }

            }
            catch (Exception ex)
            {
                return 0;
            }

            return 1;

        } 
        #endregion



         /// <summary>
         /// 筛选公告
         /// </summary>
         /// <returns></returns>
        public Object Search(int firstResult, int pagesize,string orderBy, string condition)
        {
            int userType = int.Parse(Session["UserType"].ToString());
            int totalCount = 0;  //总的数据记录条数I_NoticesType
            condition = HttpUtility.UrlDecode(condition);
            
            condition = condition + "and (I_NoticesType=" + userType + " or I_NoticesType=3) ";
            IList<TbNotices> userList = NoticesDAO.getByPageDataBase(firstResult, pagesize, orderBy, condition, out totalCount);
            return this.Json(new
            {
                DataCount = totalCount,
                Data = userList
            },JsonRequestBehavior.AllowGet);
        }


        #region 系统管理员查看公告列表
        /// <summary>
        /// 系统管理员查看公告列表 add by yzp 11.15
        /// </summary>
        /// <param name="firstResult"></param>
        /// <param name="pagesize"></param>
        /// <param name="orderBy"></param>
        /// <param name="condition"></param>
        /// <returns></returns>
        public Object DefaultSearch(int firstResult, int pagesize, string orderBy, string condition)
        {

            int totalCount = 0;  //总的数据记录条数I_NoticesType
            condition = HttpUtility.UrlDecode(condition);


            IList<TbNotices> userList = NoticesDAO.getByPageDataBase(firstResult, pagesize, orderBy, condition, out totalCount);
            return this.Json(new
            {
                DataCount = totalCount,
                Data = userList
            }, JsonRequestBehavior.AllowGet);
        }
        #endregion


    }
}
